﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using DevExpress.XtraBars.Ribbon;
using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using BartonExtension;
using Noesis.Drawing.Imaging.WebP;

namespace Backend
{
    public partial class ThumbGenerator : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private List<ThumbnailInfos<GalleryItem>> temps { get; set; }

        public ThumbGenerator()
        {
            InitializeComponent();
            temps = new List<ThumbnailInfos<GalleryItem>>();
        }

        private void btnLoad_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using(OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Multiselect = true;
                if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Utils.IndicatorShowHide(this, true);
                    gallerySource.Gallery.ImageSize = new Size(240, 160);
                    gallerySource.Gallery.ItemImageLayout = ImageLayoutMode.ZoomInside;
                    if (gallerySource.Gallery.Groups.Count == 0)
                    {
                        GalleryItemGroup group = new GalleryItemGroup();
                        group.Caption = "Source";
                        gallerySource.Gallery.Groups.Add(group);
                    }
                    using (Task task = Task.Factory.StartNew(() =>
                        {
                            dialog.FileNames.ToList<string>().ForEach(x =>
                            {
                                string ext = Path.GetExtension(x).ToLower();
                                if (ext.Equals(".jpeg") || ext.Equals(".png") || ext.Equals(".jpg") || ext.Equals(".bmp"))
                                {
                                    using(Bitmap img = new Bitmap(x))
                                    {
                                        Image thumb = img.GetThumbnailImage(240, 160, null, IntPtr.Zero);
                                        GalleryItem item = new GalleryItem(thumb, "", x);
                                        temps.Add(new ThumbnailInfos<GalleryItem>(item, ThumbnailFixedType.WIDTH_FIX, 320, new SizeF(img.Size.Width, img.Size.Height)));
                                    }
                                }
                            });
                        }))
                    {
                        task.Wait();
                        temps.ForEach(x =>
                            {
                                gallerySource.Gallery.BeginUpdate();
                                gallerySource.Gallery.Groups[0].Items.Add(x.Item);
                                gallerySource.Gallery.EndUpdate();
                            });
                        Utils.IndicatorShowHide(this, false);
                    }
                    
                }
            }
        }

        private void btnGenerate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Utils.IndicatorShowHide(this, true);
            using (Task task = Task.Factory.StartNew(() =>
            {
                temps.ForEach(x =>
                {
                    string src_path = x.Item.Description;
                    string path = Path.GetDirectoryName(src_path) + "\\Thumbs\\";
                    if(!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    ImageHelper.GetFixedRatioImage(src_path, path + Path.GetFileName(src_path), (int)x.ThumbSize.Width, (int)x.ThumbSize.Height);
                });
            }))
            {
                task.Wait();
                Utils.IndicatorShowHide(this, false);
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<GalleryItem> items = gallerySource.Gallery.GetCheckedItems();
            items.ForEach(x =>
                {
                    int index = temps.FindIndex(k =>
                        {
                            return k.Item.Equals(x);
                        });
                    temps.RemoveAt(index);
                    gallerySource.Gallery.Groups[0].Items.Remove(x);
                });
        }

        private void ThumbGenerator_FormClosing(object sender, FormClosingEventArgs e)
        {
            temps.Clear();
        }
    }
}